﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Chapter14.Code07.Code04
{
    using System.Timers;

    public class Demo
    {
        private delegate int TwoIntegerOperationDerationDelegate(int paramA, int paramB);

        public static void Begin()
        {
            {
                Timer myTimer = new Timer(100);
                myTimer.Elapsed += (object sender, ElapsedEventArgs e) =>
                    {
                        Console.WriteLine("Event handler called after {0} milliseconds.", (sender as Timer).Interval);
                    };
            }
            {
                PerformOperations((paramA, paramB) => paramA + paramB);
            }
            {
                PerformOperations(delegate(int paramA, int paramB)
                {
                    return paramA + paramB;
                });
            }
            {
                PerformOperations((paramA, paramB) =>
                {
                    return paramA + paramB;
                });
            }
        }

        private static void PerformOperations(TwoIntegerOperationDerationDelegate del)
        {
            for (int paramAVal = 1; paramAVal <= 5; paramAVal++)
            {
                for (int paramBVal = 1; paramBVal <= 5; paramBVal++)
                {
                    int delegateCallResult = del(paramAVal, paramBVal);
                    Console.Write("f({0},{1})={2}", paramAVal, paramBVal, delegateCallResult);
                    if (paramBVal != 5)
                    {
                        Console.Write(", ");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
